

private["_keydown_Help", "_keydown_NightVision", "_keydown_camconstruct", "_keydown_switchCamera" , "_keydown_space", "_mousez", "_playableUnits", "_z","_captiveon"];



_pos = _this select 1;

_camx = _pos select 0;
_camy = _pos select 1;
_camz = _pos select 2;

if(captive user1) then
{
	_captiveon = true;
}else{
	_captiveon = false;
	_code={
			user1 allowDamage false;
			user1 setCaptive true;
			};
	[_code] execvm "scripts\VTS_Functions\runcodeonallclients.sqf";
};

VTS_Freecam_nvg = false;
VTS_Freecam_on = true;
VTS_Freecam_help = false;
VTS_Freecam_players = [];
_playableUnits = playableUnits;
VTS_Freecam_target = player;
{if (alive _x) then {VTS_Freecam_players = VTS_Freecam_players + [_x]}} foreach _playableUnits;
VTS_Freecam_players_count = count VTS_Freecam_players - 1;
VTS_Freecam_players_select = 0; //waitUntil {not alive player}; sleep 5; //cutText ["GDT SPECTATOR\n----------------------------------------------------------------------------------------------\nWill start shortly\n\nDuring spectating press your help button (Standard: H) for instructions","PLAIN"]; //waitUntil {typeof cameraOn == "SeaGull"}; //cutText ["GDT SPECTATOR\n----------------------------------------------------------------------------------------------\nWill start shortly\n\nDuring spectating press your help button (Standard: H) for instructions","PLAIN"]; 
stopcam = false;
//setAperture -1;
//hint "1";
VTS_Freecam = "camconstruct" camcreate [_camx,_camy-5,_camz+10];
VTS_Freecam camConstuctionSetParams [[_camx,_camy,_camz], 20000, 10000];
VTS_Freecam camSetTarget player; VTS_Freecam cameraeffect ["internal","back"];
	
_keydown_Help = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) in (actionkeys ""Help"")) then {
		if (not VTS_Freecam_help) then {
			titleText [""Mousewheel:                     Camera jumps to player positions\nMovement keys and mouse:                          Move camera\n\nQ:                                              Move camera up\nY or Z:                                      Move camera down\nSHIFT Left:                                      Slow movement\n\nNightvision key:                              Nightvision On/Off\n\nPress Space to exit."",""PLAIN""];
			VTS_Freecam_help = true;
		}
		else {
			titleText ["""",""PLAIN""];
			VTS_Freecam_help = false;
		};
	};
"];



_keydown_NightVision = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) in (actionkeys ""NightVision"")) then {
		if (VTS_Freecam_nvg) then {camUseNVG false;VTS_Freecam_nvg = false;}
		else {camUseNVG true;VTS_Freecam_nvg = true;};
	};
"];



_keydown_space = (finddisplay 46) displayaddeventhandler ["keydown", "
	if ((_this select 1) == 57 ) then {
		stopcam = true;
	};
"];


_mousez = (findDisplay 46) displayAddEventHandler ["mousezchanged", "
	if ({alive _x} count VTS_Freecam_players > 0) then {
		_z = _this select 1;
		if (_z  < 0) then {
			VTS_Freecam_players_select = VTS_Freecam_players_select - 1;
			if (VTS_Freecam_players_select < 0) then {VTS_Freecam_players_select = VTS_Freecam_players_count};
			while {not (alive (VTS_Freecam_players select VTS_Freecam_players_select))} do {
				VTS_Freecam_players_select = VTS_Freecam_players_select - 1;
				if (VTS_Freecam_players_select < 0) then {VTS_Freecam_players_select = VTS_Freecam_players_count};
			};
			VTS_Freecam_target = vehicle (VTS_Freecam_players select VTS_Freecam_players_select);		
		}
		else {
			VTS_Freecam_players_select = VTS_Freecam_players_select + 1;
			if (VTS_Freecam_players_select > VTS_Freecam_players_count) then {VTS_Freecam_players_select = 0};
			while {not (alive (VTS_Freecam_players select VTS_Freecam_players_select))} do {
				VTS_Freecam_players_select = VTS_Freecam_players_select + 1;
				if (VTS_Freecam_players_select > VTS_Freecam_players_count) then {VTS_Freecam_players_select = 0};
			};
			VTS_Freecam_target = vehicle (VTS_Freecam_players select VTS_Freecam_players_select);	
		};
		cutText [format[""%1"", name VTS_Freecam_target],""PLAIN""];
		if (VTS_Freecam_on) then {
		VTS_Freecam setpos [((position VTS_Freecam_target) select 0) + ((sin (getdir VTS_Freecam_target)) * ( - 10)), ((position VTS_Freecam_target) select 1) + ((cos (getdir VTS_Freecam_target)) * (- 10)), ((position VTS_Freecam_target) select 2) + 10];
		
		VTS_Freecam setdir getdir VTS_Freecam_target;
		VTS_Freecam camSetTarget VTS_Freecam_target;
		}
		else {VTS_Freecam_target switchCamera ""EXTERNAL"";};
		VTS_Freecam_help = false;

	};
"];

titleText ["Press H for Help and Space to quit", "PLAIN"];
//sleep 5;
//titleFadeOut 1;

//lapin setpos (position VTS_Freecam_target) ;

waitUntil {stopcam};

if(_captiveon) then
{
}else{
	user1 allowDamage true;
	user1 setCaptive false;
	_code={
		user1 allowDamage true;
		user1 setCaptive false;
		};
	[_code] execvm "scripts\VTS_Functions\runcodeonallclients.sqf";
};

VTS_Freecam_target switchCamera "INTERNAL";
VTS_Freecam CameraEffect ["Terminate","Back"];
CamDestroy VTS_Freecam;
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_Help];
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_NightVision];
(finddisplay 46) displayRemoveEventHandler ["keydown",_keydown_space];
(finddisplay 46) displayRemoveEventHandler ["mousezchanged",_mousez];


[] execVM "Computer\cpu_dialog.sqf";
